function DamPopup() {
	var _self = this;
	_self.taskPool = new TaskPool();

	_self.onContentLoaded = function() {
		// initialize task pool
		_self.taskPool.initTaskPool();
		// render the tasks
		_renderTasks();
		// attach events
		_attachEvents();
	};

	var _renderTasks = function() {
		var listHtml = "";
		//var tasks = _self.taskPool.getAllTask();
		var tasks = _self.taskPool.getTodayTask();
		for ( var i = 0; i < tasks.length; i++) {
			listHtml += _oneTaskItem(tasks[i]);
		}
		jQuery(".taskList").empty().html(listHtml);
	};

	var _oneTaskItem = function(item) {
		var divHtml = '<div class="taskItem">';
		divHtml += '<div class="fl tTime">' + item.getSimpleTaskTime() + '</div>';
		divHtml += '<div class="fl tDesc">' + item.getDesc() + '</div>';

		// task item operate icons
		divHtml += '<div class="fl tIcons" style="display:none">';
		divHtml += '<div class="fl tFinish">Finish</div>';
		divHtml += '<div class="fl tEdit">Edit</div>';
		divHtml += '<div class="fl tDelete">Delete</div>';
		divHtml += '</div>';

		divHtml += '</div>';
		return divHtml;
	};

	var _taskMouseOverEvent = function() {
		jQuery(".tIcons", this).css("display", "block");
	};

	var _taskMouseOutEvent = function() {
		jQuery(".tIcons", this).css("display", "none");
	};

	var _newTaskEvent = function() {
		var html = '<div class="taskNewItem">';
		html += '<div class="fl tTime">time:<input style="width: 50px;" type="text" placeholder="hh:mm" /></div>';
		html += '<div class="fl tDesc">desc:<input style="width: 130px;" type="text" placeholder="" /></div>';
		html += '<input id="addTaskBtn" type="button" value="OK"/></div>';
		jQuery(".taskList").append(html);
		jQuery(".tTime input").focus();
		jQuery(".newTaskDiv").css("display", "none");
	};

	var _taskSave = function() {
		var newDesc = jQuery(".taskNewItem .tDesc input").val();
		var newAlertTime = jQuery(".taskNewItem .tTime input").val();
		
		var at = new Date();
		at.setHours(newAlertTime.split(":")[0]);
		at.setMinutes(newAlertTime.split(":")[1]);
		at.setSeconds(0);
		
		var option = {
			alertTime : at,
			desc : newDesc
		};
		var ti = new TaskItem(option);
		// save new task
		_self.taskPool.addTask(ti);
		// re-render tasks
		_renderTasks();
		jQuery(".newTaskDiv").css("display", "block");
	};

	var _attachEvents = function() {
		/*jQuery(".taskList").delegate(".taskItem", "mouseover",
				_taskMouseOverEvent);
		jQuery(".taskList").delegate(".taskItem", "mouseout",
				_taskMouseOutEvent);*/
		jQuery(".newTaskDiv").unbind("click").bind("click", _newTaskEvent);
		jQuery(".taskList").delegate("#addTaskBtn", "click", _taskSave);
	};

};
window.damPopup = new DamPopup();
document.addEventListener("DOMContentLoaded", function() {
	window.damPopup.onContentLoaded();
});
// jQuery(document).ready(window.damPopup.onContentLoaded); // Not work for
// chrome plugin
